﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKArchMethodLibrary.Utils;

namespace SectionSignAndFilled.StairsClearHeightDimension
{
	public class DimesionDataManager
	{
		public DimesionDataManager(ElementId idDimView, XYZ ptStart, XYZ ptEnd, ElementId dimTypeId, XYZ vecDimDir)
		{
			this.m_idDimView = idDimView;
			this.m_ptDimStart = ptStart;
			this.m_ptDimEnd = ptEnd;
			this.m_idDimType = dimTypeId;
			this.m_vecLineDir = vecDimDir;
		}

		public ElementId CreateDimsion(Autodesk.Revit.DB.Document doc)
		{
			ElementId result = ElementId.InvalidElementId;
			if (null == this.m_idDimType || null == this.m_idDimView || this.m_vecLineDir == null || this.m_ptDimStart == null || this.m_ptDimEnd == null || Geometry.LessThan(this.m_ptDimStart.DistanceTo(this.m_ptDimEnd), 1E-05))
			{
				return result;
			}
			int num = doc.ActiveView.get_Parameter(BuiltInParameter.VIEW_SCALE).AsInteger();
			double num2 = 0.0;
			DimensionType dimensionType = doc.GetElement(this.m_idDimType) as DimensionType;
			if (dimensionType != null)
			{
				Parameter parameter = dimensionType.GetParameter(this.m_strSizeLength);
				if (parameter != null)
				{
					num2 = Common.FeetToMM(parameter.AsDouble());
				}
			}
			View view = doc.GetElement(this.m_idDimView) as View;
			if (view == null)
			{
				return result;
			}
			double num3 = Common.MMToFeet(num2 * (double)num);
			double num4 = Common.MMToFeet(1.0);
			XYZ xyz = this.m_vecLineDir.Normalize();
			XYZ xyz2 = -xyz.Multiply(num3);
			XYZ ptDimStart = this.m_ptDimStart;
			XYZ xyz3 = ptDimStart + xyz.Multiply(num4);
			XYZ ptDimEnd = this.m_ptDimEnd;
			XYZ xyz4 = ptDimEnd + xyz.Multiply(num4);
			Line line = Line.CreateBound(ptDimStart, xyz3);
			Line line2 = Line.CreateBound(ptDimEnd, xyz4);
			Line line3 = Line.CreateBound(this.m_ptDimStart + xyz2, this.m_ptDimEnd + xyz2);
			ReferenceArray referenceArray = new ReferenceArray();
			List<ElementId> list = new List<ElementId>();
			DetailLine detailLine = doc.Create.NewDetailCurve(view, line) as DetailLine;
			DetailLine detailLine2 = doc.Create.NewDetailCurve(view, line2) as DetailLine;
			Reference reference = detailLine.GeometryCurve.Reference;
			Reference reference2 = detailLine2.GeometryCurve.Reference;
			referenceArray.Append(reference);
			referenceArray.Append(reference2);
			list.Add(detailLine.Id);
			list.Add(detailLine2.Id);
			Dimension dimension = doc.Create.NewDimension(view, line3, referenceArray, dimensionType);
			result = dimension.Id;
			StairsDimXDataMgr.WriteDimensionXData(dimension, list);
			return result;
		}

		private XYZ m_ptDimStart;

		private XYZ m_ptDimEnd;

		private ElementId m_idDimType;

		private XYZ m_vecLineDir;

		private ElementId m_idDimView;

		private string m_strSizeLength = "尺寸界线长度";
	}
}
